<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="菜单列表"
      row-key="_id"
      :pagination="false"
      :indent="20"
      :highlight-current-row="true"
      :columns="columns"
      :expand-all="true"
      :request-api="getTableList"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增一级')"> 新增一级 </el-button>
        <el-button
          type="primary"
          plain
          :icon="CirclePlus"
          :disabled="!scope.isSelected"
          @click="openDrawer('新增下级', {}, scope.selectedList)"
        >
          新增下级
        </el-button>
      </template>
      <!-- 菜单图标 -->
      <template #icon="scope">
        <el-icon :size="18">
          <component :is="scope.row.meta.icon"></component>
        </el-icon>
      </template>
      <!-- 菜单操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)"> 编辑 </el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteData(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <MenuDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="menuMange">
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, CirclePlus } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import apiService from "@/api/services/apiService";
import { useHandleData } from "@/hooks/useHandleData";
import MenuDrawer from "./menuDrawer.vue";
import { ref } from "vue";

const proTable = ref();

const getTableList = (params: any) => {
  params.options.sort = {
    sort: 1
  };
  return apiService.findTree("menus", params.condition, params.options);
};

// 表格配置项
const columns: ColumnProps[] = [
  { type: "index", label: " ", width: 85 },
  { prop: "title", label: "菜单标题", align: "left", search: { el: "input" } },
  { prop: "meta.icon", label: "菜单图标", width: 100 },
  { prop: "name", label: "菜单名称", search: { el: "input" } },
  { prop: "path", label: "菜单路径", search: { el: "input" } },
  { prop: "meta.isLink", label: "外部链接", width: 200 },
  { prop: "component", label: "组件路径" },
  { prop: "sort", label: "序号", width: 100 },
  {
    prop: "public",
    label: "公开",
    width: 100,
    search: { el: "select" },
    enum: () => apiService.findClassify("IS_NOT"),
    fieldNames: { label: "name", value: "value" }
  },
  { prop: "meta.isAffix", label: "固定首页", width: 100 },
  { prop: "operation", label: "操作", width: 180, fixed: "right" }
];

// 删除用户信息
const deleteData = async (params: Menu.MenuOptions) => {
  await useHandleData(apiService.deleteById, "menus", params._id, `删除参数【${params.name}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof MenuDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Menu.MenuOptions> = {}, selectList: { [key: string]: any }[] = []) => {
  if (title.indexOf("新增") !== -1) {
    selectList?.length ? (row.pid = selectList[0]._id) : delete row.pid;
    row.meta = {
      icon: "",
      title: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true
    };
  }
  const params = {
    title,
    tbName: "menus",
    isView: title === "查看",
    row: { ...row },
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
